package com.topView2;

import com.bean.ListNode;

/**
 * @Author: huangzhigao
 * @Date: 2023/2/12 13:39
 */
public class leecode21 {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode dummy = new ListNode();
        ListNode p1 = list1;
        ListNode p2 = list2;
        ListNode cur = dummy;
        while (p1 != null && p2 != null) {
            if(p1.val>p2.val){
                cur.next = new ListNode(p2.val);
                p2 = p2.next;
            }else{
                cur.next = new ListNode(p1.val);
                p1 = p1.next;
            }
            cur = cur.next;
        }

        while (p1!=null){
            cur.next = new ListNode(p1.val);
            p1 = p1.next;
            cur = cur.next;
        }
        while (p2!=null){
            cur.next = new ListNode(p2.val);
            list2 = list2.next;
            cur = cur.next;
        }
        return dummy.next;
    }
}
